//
/**
 * Filename: fig10-11_timeout_read2.c
 * Author:   csbonkers
 * Contact:  forever65535@qq.com
 * Date:     2021/6/19
 * Description:
 *    
 */
#include "apue.h"
#include <setjmp.h>

static jmp_buf env_alrm;
static void sig_alrm(int);

int main(int argc, char *argv[]) {
  char line[MAXLINE];
  if(signal(SIGALRM, sig_alrm) == SIG_ERR){
    err_sys("signal(SIGALRM) error");
  }
  if(setjmp(env_alrm) != 0){
    err_quit("read timeout");
  }
  alarm(10);
  int n;
  if((n = read(STDIN_FILENO,line,MAXLINE)) <0){
    err_sys("read error");
  }
  alarm(0);
  write(STDOUT_FILENO, line, n);
  return 0;
}

static void sig_alrm(int signo){
  longjmp(env_alrm, 1);
}